package com.bosi.dalian.service;

import com.bosi.dalian.config.DataSourceConfig;
import com.bosi.dalian.config.DataSourceContextHolder;
import com.bosi.dalian.config.DataSourceSet;
import com.bosi.dalian.entity.Busi;
import com.bosi.dalian.entity.DaLian;
import com.bosi.dalian.repository.busi.BusiRepostory;
import com.bosi.dalian.repository.dl.DlRepostory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.Query;

@Service
@Transactional
public class DaLianService {

    @Autowired
    private BusiRepostory busiRepostory;

    @Autowired
    private DlRepostory dlRepostory;

    @DataSourceSet(DataSourceConfig.BUSI_DATASOURCE_KEY)
    public void saveBusi(){
        Busi busi = new Busi();
        busi.setId("busi");
        busi.setName("busi");

        busiRepostory.save(busi);

    }

    @Autowired
    @Qualifier("entityManagerFactoryBean")
    private EntityManager dlEntityManager;

    @DataSourceSet(DataSourceConfig.DL_DATASOURCE_KEY)
    public void saveDalian(){
        DaLian daLian = new DaLian();
        daLian.setId("daLian");
        daLian.setName("daLian");
        dlRepostory.save(daLian);

        String sql = "INSERT INTO DL_TEST VALUES('123132','321321')";
        Query query = dlEntityManager.createNativeQuery(sql);
        query.executeUpdate();

        String sql2 = "INSERT INTO BUSI_TEST VALUES('123132','321321')";
        Query query2 = dlEntityManager.createNativeQuery(sql2);
        query2.executeUpdate();
    }
}



